package com.aier.admin_new.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.entity.SchoolTeacherNew;
import com.aier.admin_new.entity.TreeNode;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolGrade;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.common.UserFinalPageSize;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;
import com.google.gson.JsonArray;

@Scope("prototype")
@Component("schoolChartAction")
public class SchoolChartAction extends AdminNewBaseAction {
	
	private int sid;
	private long stid;//
	private long gid;//
	private String id;//当前点击的节点ID
	//页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    //错误码
    private int s;
    
	@Resource(name = "userService")
	private IUserService userService;
	@Resource(name = "schoolService")
	private ISchoolService schoolService;
	@Resource(name = "schoolTeacherService")
	private ISchoolTeacherService schoolTeacherService;
	
	List<TreeNode> treeList;//返回的树形json数据
    // 教师列表
    private List<SchoolTeacherNew> teacherList;
	
	@Override
	public String execute() throws Exception {
		School school = schoolService.searchSchoolBySid(sid);
		return super.execute();
	}
	
	  public String getZtreeData(){ 
		  treeList =new ArrayList<TreeNode>();
		  School school = schoolService.searchSchoolBySid(Integer.parseInt(id));
		  TreeNode  tr = new TreeNode();
		  tr.setId(id);
		  tr.setName(school.getName());
		  tr.setHasChild(true);
		  tr.setIsParent(true);
		  tr.setpId("0");
		  treeList.add(tr);
		  List<SchoolGrade> listG=schoolService.searchSchoolGradeList(Integer.parseInt(id));
		  for(SchoolGrade  sg : listG){
			  tr = new TreeNode();
			  tr.setId(sg.getGid().toString());
			  tr.setName(sg.getName());
			  tr.setpId(id);
			  tr.setIsParent(true);
			  tr.setHasChild(true);
			  treeList.add(tr);
			  List<SchoolTeacher> listt =schoolTeacherService.selectTeacher(Integer.parseInt(id), "", sg.getGid());
			  
			  for(SchoolTeacher str : listt){
				  tr = new TreeNode();
				  tr.setId(str.getTid().toString());//教师ID
				  tr.setName(str.getTname());//教师名
				  tr.setpId(sg.getGid().toString());
				  tr.setIsParent(false);
				  tr.setHasChild(false);
				  treeList.add(tr);
			  }
		  }
		  return SUCCESS;
	  }
	  
	  
	  
	  public String getSchoolTeahersList(){
		  if (page <= 0) {
	            page = 1;
	        }
		  
		 	Map<String, Object> map = null;
		 	
	        map = schoolTeacherService.searchSchoolTeacherNew(sid, stid, null , page, SchoolFinalPageSize.CLASSES_MAINTAIN_LIST_PAGESIZE);
	       
	        List<SchoolTeacher> list = (List<SchoolTeacher>) map.get(ICommonDAO.OBJ_LIST);
	        teacherList = new ArrayList<>();
	        for(int i =0 ;i<list.size() ;i++){
	        	SchoolTeacherNew stn = new SchoolTeacherNew();
	        	SchoolTeacher ster =	list.get(i);
	        	stn.setTid(ster.getTid());
	        	stn.setGname(ster.getGname());
	        	stn.setPhone(ster.getAccount());
	        	stn.setTname(ster.getTname());
	        	//获取权限
	        	String rname="";
	        	stn.setRname(rname);
	        	teacherList.add(stn);
	        }
	        
	        if(teacherList==null){
	        	s=0;
	        }
	        
	        page = (Integer) map.get(ICommonDAO.PAGE);
	        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
	        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
	        count = (Integer) map.get(ICommonDAO.COUNT);
	        s = 1;
		  return SUCCESS;
	  }

	public void setStid(long stid) {
		this.stid = stid;
	}

	public void setGid(long gid) {
		this.gid = gid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public int getSid() {
		return sid;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<TreeNode> getTreeList() {
		return treeList;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage_size() {
		return page_size;
	}

	public int getPage_count() {
		return page_count;
	}

	public int getCount() {
		return count;
	}
}
